import argparse
from PIL import Image
import os
import requests, base64, uuid, random, time


parser = argparse.ArgumentParser(description='Info')
parser.add_argument("--tag",type=str,default='***')
parser.add_argument("--p",type=float,default=1.2)

try:
    args = parser.parse_args()
    resp =  requests.get('https://wakuwaku.azurewebsites.net/api/ai?tag=%s&seed=%s&x=1' % (args.tag, random.randint(1, 9999999)))                      # 获取网页数据
    img = base64.b64decode(resp.text + '=')             # base64 解码为图片
    uui = 'image'

    path = './plugins/ai-draw/image/%s.png' % uui
    
    f = open(path, 'wb')                       # out to path
    f.write(img)
    f.close()
    
    if args.p == -1:
        print(path)
        exit()
    else:
        print(path)
    im = Image.open(path)
    (x, y) = im.size
    xx = int(x/float(args.p))
    yy = int(y/float(args.p))
    out = im.resize((xx, yy), Image.Resampling.LANCZOS)
    os.remove(path)
    out.save(path)
    time.sleep(1)
    exit()
except Exception as e:
    print(e)